﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Xml.XPath;
using System.Xml;
using Microsoft.WindowsAzure.ServiceRuntime;
using System.IO;

namespace SaaS
{
    /// <summary>
    /// Summary description for searchOntology
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class searchOntology : System.Web.Services.WebService
    {
        [WebMethod]
        public HashSet<string> findService(List<string> searchName)
        {
            //LocalResource myStorage = RoleEnvironment.GetLocalResource("TempStorage");
            //string serviceOntology = Path.Combine(myStorage.RootPath, "serviceOntology.xml");
            //string serviceOntology = Path.Combine("https://minisaas.blob.core.windows.net/appdata/", "serviceOntology.xml");
        

            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load("https://minisaas.blob.core.windows.net/appdata/serviceOntology.xml");

            HashSet<string> serviceName = new HashSet<string>();

            foreach (string name in searchName)
            {

                HashSet<string> newServiceName = searchXML(doc, name);
                serviceName.UnionWith(newServiceName);

            }

            return serviceName;
        }

        public HashSet<string> searchXML(XmlDocument doc, string name)
        {

            HashSet<string> idList = new HashSet<string>();

            //search for category name, return all the services under this category
            XmlNodeList xnl = doc.SelectNodes("//Category");
            foreach (XmlNode xn in xnl)
            {
                if (xn.FirstChild.InnerText.ToString() == name)
                {
                    XmlNodeList serviceList = xn.SelectNodes(".//Service/id");
                    foreach (XmlNode serviceId in serviceList)
                    {
                        idList.Add(serviceId.InnerText.ToString());
                    }
                }

            }
            //search for sub-category name, return all the services under this category
            xnl = doc.SelectNodes("//SubCategory");
            foreach (XmlNode xn in xnl)
            {
                if (xn.FirstChild.InnerText.ToString() == name)
                {
                    XmlNodeList serviceList = xn.SelectNodes(".//Service/id");
                    foreach (XmlNode serviceId in serviceList)
                    {
                        idList.Add(serviceId.InnerText.ToString());
                    }
                }

            }

            //search for service name
            xnl = doc.SelectNodes("//Service");
            foreach (XmlNode xn in xnl)
            {
                string serviceName = xn.SelectSingleNode("./Name").InnerText.ToString();
                if (serviceName == name)
                {
                    idList.Add(xn.SelectSingleNode("./id").InnerText.ToString());                    
                }

            }

            return idList;

        }
    }

   
}
